<?php

namespace teach\controllers;

use teach\components\WeChat;
use teach\service\Calculation;
use teach\service\SendWechatMessage;
use teach\service\StarModel;
use teach\service\User;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

class UserController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 用户信息
     * @return \yii\web\Response
     */
    public function actionInfo()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 用户id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $mapper = " status = 1 AND id = {$uid} ";
            $map = " status = 1 AND uid = {$uid} ";


            $users = new User();
            $user = $users->userMsg($mapper);
            if (!$user) {
                $jsonData = ResponseHelper::buildJsonData(400, '发生错误,联系管理员');
                return $this->asJson($jsonData);
            }
            //判断用户是否是商家
            if ($user['mall_is'] == 1) {

                //如果是商家 mall_id = id
                $user['mall_id'] = $user['id'];
                //商家
                $param = " mid = {$uid} AND status != -1 AND status !=4  AND status != -2";

                //今日订单
                $user['today_order_count'] = $users->userTodayOrderCount($param);

                //今日收益
                $user['today_profit'] = $users->userTodayProfit($map);

                //今日营业额
                $user['today_turnover'] = $users->userTodayOrderTurnover($param);

                //累计订单
                $user['order_count'] = $users->userOrderCount($param);

                //累计收益
                $user['profit'] = $users->userProfit($map);

                //累计营业额
                $user['turnover'] = $users->userOrderTurnover($param);

                //商家店铺订单参数
                //待发货 说明已经支付 status = 1 mid = uid

                $parameter = " status = 1 AND mid = {$uid} ";
                $user['stay_deliver'] = $users->orderStatus($parameter);

                //待付款
                $parameters = " status = -1 AND mid = {$uid} ";
                $user['stay_pay'] = $users->orderStatus($parameters);

                //待收货
                $parameterName = " status = 2 AND mid = {$uid} ";
                $user['stay_receive'] = $users->orderStatus($parameterName);

                $user['star'] = 5;


            } else {
                $param = " uid = {$uid} AND status != -1 AND status !=4 AND status != -2 ";
                //普通用户
                //今日订单
                $user['today_order_count'] = $users->userTodayOrderCount($param);
                //今日收益
                $user['today_profit'] = $users->userTodayProfit($map);

                //累计订单数量
                $user['order_count'] = $users->userOrderCount($param);

                //累计收益
                $user['profit'] = $users->userProfit($map);

                //TODO 要跳转的appid
                $user['appid'] = 'appid占位';

                //获取规则
                $distribution = $users->distribution($user['mall_id']);
                if ($distribution) {
                    $user['mode'] = $distribution['state'];
                    switch ($user['score_total'])
                    {
                        case $user['score_total'] >= 0 && $user['score_total'] < $distribution['three_star_score']:
                            $user['star'] = $distribution['three_star'];
                            $user['next_star_score'] = $distribution['three_star_score'];
                            break;
                        case $user['score_total'] < $distribution['five_star_score']:
                            $user['star'] = $distribution['three_star'];
                            $user['next_star_score'] = $distribution['five_star_score'];
                            break;
                        case $user['score_total'] < $distribution['six_star_score']:
                            $user['star'] = $distribution['five_star'];
                            $user['next_star_score'] = $distribution['six_star_score'];
                            break;
                        case $user['score_total'] < $distribution['seven_star_score']:
                            $user['star'] = $distribution['six_star'];
                            $user['next_star_score'] = $distribution['seven_star_score'];
                            break;
                        case $user['score_total'] >= $distribution['seven_star_score']:
                            $user['star'] = $distribution['seven_star'];
                            $user['next_star_score'] = $distribution['seven_star_score'];
                            break;
                        default:
                            $user['star'] = 3;
                    }
                } else {
                    $user['star'] = 3;

                }

            }

            $jsonData = ResponseHelper::buildJsonData(200, '成功', $user);
        }

        return $this->asJson($jsonData);
    }

    /**
     * 修改真实姓名
     * @return \yii\web\Response
     */
    public function actionReal()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $mall_rname = $request->post('mall_rname');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 用户id不能为空';
        } elseif (!$mall_rname) {
            $msg = 'mall_rname 真实姓名不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $users = new User();

            $map = " id = " . $uid;
            $param = " mall_rname = '{$mall_rname}'";

            $res = $users->userRealName($map, $param);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $res);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败', $res);
            }

        }

        return $this->asJson($jsonData);
    }

    /**
     * 资金管理
     * @return \yii\web\Response
     */
    public function actionFund()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 用户id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $users = new User();

            $map = " id = {$uid} ";
            //获取用户资金
            $user = $users->userSum($map);

            //从分佣表中获取今天分佣的金额
            $param = " status = 1 AND uid = {$uid} ";
            $todayProfit = $users->userTodayProfit($param);

            $user['today_profit'] = $todayProfit;

            //从分佣表中获取全部分佣的金额
            $profit = $users->userProfit($param);
            $user['profit'] = $profit;

            //从订单表中获取未结算的订单金额
            $where = " status = 2 AND uid = {$uid} ";
            $stay_count = $users->userStayCount($where);
            $user['stay_count'] = $stay_count;

            $jsonData = ResponseHelper::buildJsonData(200, '成功', $user);
        }

        return $this->asJson($jsonData);

    }

    /**
     * 提现
     * @return \yii\web\Response
     */
    public function actionCash()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $amount = $request->post('amount');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$amount) {
            $msg = 'amount 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $users = new User();

            //根据id查找用户
            $param = " id = " . $uid;
            $user = $users->userMsg($param);

            if ($user) {
                //判断用户是否有这么多金额
                if (floatval($user['mall_total_fee']) < floatval($amount) || floatval($amount) < 1) {
                    $jsonData = ResponseHelper::buildJsonData(401, '对不起，您的金额有误');
                    return $this->asJson($jsonData);
                }
                //用户存在则入库
                $map = [
                    'openid' => $user['openid'],
                    'uid' => $uid,
                    'amount' => $amount,
                    'status' => 1,
                    'create_time' => time()
                ];
                $res = $users->userAppear($map);
                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '成功');
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '申请失败,请及时联系商家');

                }
            } else {
                //用户不存在返回401
                $jsonData = ResponseHelper::buildJsonData(401, '用户有误');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * 收入明细
     * @return \yii\web\Response
     */
    public function actionIndetail()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $map = " status = 1 AND uid = {$uid}";
            $users = new User();

            $incomeList = $users->userIncomeDetail($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $incomeList);
        }

        return $this->asJson($jsonData);
    }

    /**
     * 提现记录
     * @return string
     */
    public function actionRecord()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;

        if (!$uid) {
            $msg = 'uid 用户ID不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $map = " status = 1 AND uid = {$uid} ";
            $users = new User();

            $cashList = $users->userCashRecord($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $cashList);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 获取用户订单列表 -1未支付 0 获取全部 1待发货 2待收货 3已完成 -2 申请退款 -3 退款成功 -6退款拒绝
     * @return \yii\web\Response
     */
    public function actionOrders()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $status = $request->post('status');
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;
        if (empty(isset($uid))) {
            $msg = 'uid 用户id不能为空';
        } elseif (empty(isset($status))) {
            $msg = 'status 状态不能为空';
        } elseif (empty(isset($mid))) {
            $msg = 'mid 不能为空 ';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;

            //站在用户角度查询订单
            if ($uid != 0 && $mid == 0) {
                $map = " uid = {$uid} ";
            } elseif ($uid == 0 && $mid != 0) {
                $map = " mid = {$mid} ";
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '参数错误');
                return $this->asJson($jsonData);
            }
            if ($status == -1) {
                $map .= " AND status = -1  ";
            }
            if ($status == 0) {
                $map .= " AND (status = -6 OR status = -3 OR status = -2 OR status = -1 OR status = -2 OR status = 1 OR status = 2 OR status = 3)";
            }
            if ($status == 1) {
                $map .= " AND status = 1";
            }
            if ($status == 2) {
                $map .= " AND status = 2";
            }
            if ($status == 3) {
                $map .= " AND status = 3";
            }
            if ($status == -2) {
                $map .=" AND status = -2";
            }
            if ($status == -3) {
                $map .= " AND status = -3";
            }
            if ($status == -6) {
                $map .= " AND status = -6";
            }
            $users = new User();
            $orderList = $users->orderList($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $orderList);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 用户订单操作
     * 2 点击发货 3确认收货 4取消订单
     * @return \yii\web\Response
     */
    public function actionOperate()
    {
        $request = Yii::$app->request;
        $oid = $request->post('oid');
        $mid = $request->post('mid');
        $uid = $request->post('uid');
        $state = $request->post('state');

        $msg = '';
        $flag = false;
        if (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$state) {
            $msg = 'state 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $users = new User();
            $time = time();
            $map = " status = {$state} ";
            if ($state == 2) {
                $map .= " , send_time = {$time} ";
            }
            if ($state == 3) {
                $map .= " , finish_time = {$time} ";
            }
            $param = " id = {$oid} AND mid = {$mid} AND uid = {$uid} ";

            //当传的参数不是2,3,4时，都是非合法参数
            if (!in_array($state, [2, 3, 4])) {
                $jsonData = ResponseHelper::buildJsonData(400, '请勿非法操作订单!');
                return $this->asJson($jsonData);
            }
            //当state = 2 时查看一下订单是不是=1 是属于支付状态， 否则就是非法操作
            if ($state == 2) {
                $order = $users->orderOne($param);

                //订单不存在，无法操作
                if (!$order) {
                    $jsonData = ResponseHelper::buildJsonData(400, '订单不存在,非法操作!');
                    return $this->asJson($jsonData);
                }

                //当订单不等于1时不可以直接修改为发货
                if ($order['status'] != 1) {
                    $jsonData = ResponseHelper::buildJsonData(400, '请勿非法操作订单!');
                    return $this->asJson($jsonData);
                }

            }

            //TODO 将订单修改为收货时，加入完成时间

            $res = $users->orderChange($map, $param);

            if ($res) {
                if ($state == 3) {
                    //查找出订单号
                    $order = $users->orderOne($param);
                    $out_trade_no = $order['out_trade_no'];
                    $orderList = $users->orderListAll($out_trade_no);
                    //循环给产品增加售卖量
                    foreach ($orderList as $_key => $_val) {
                        $map = " count_sale = count_sale + {$_val['count']}";
                        $where = " id = {$_val['pid']}";
                        $users->productSaleAdd($map, $where);
                    }

                    //计算佣金分成
                    //$welfare = new Welfare();
                    //$welfare->payAll($oid, $mid, $uid);

                    //查找订单号，给用户增加佣金，然后将记录改为已结算 //用户点击收货确认后 用自动脚本给用户计算佣金跟入账 不在点击确认时转入佣金
                    //$filterWhere = " uid = {$uid} AND mid = {$mid} AND find_in_set({$out_trade_no}, out_trade_no) ";
                    //$calculation = new Calculation();
                    //$calculation->add($oid, $uid, $mid, $out_trade_no);

                    //发送收货小程序消息
                    $this->finishGood($order);
                }
                $jsonData = ResponseHelper::buildJsonData(200, '修改成功');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '修改失败');
            }
        }

        return $this->asJson($jsonData);
    }

    public function actionUncalTest()
    {
        //未结算测试
        $starModel = new StarModel();
        $oid = 1319;
        $mid = 10000;
        $uid= 10586;
        var_dump($oid);
        var_dump($starModel->payAll($oid, $mid,$uid));

    }

    public function actionCalTest()
    {
        //结算测试
        $calculation = new Calculation();
        $oid = 11;
        $uid = 3;
        $mid = 1;
        $out_trade_no = '20190304155170584706655';
        $calculation->add($oid, $uid,$mid, $out_trade_no);

    }

    /**
     * @param $order
     * 给商家跟用户发送收货成功的消息
     */
    private function finishGood($order)
    {
        $redis = Yii::$app->redis;
        $SendWeChatMessage = new SendWechatMessage();
        //告诉用户已经发货
        $tip = "收货成功";
        $WeChat = new WeChat();
        while (true) {
            $formid = $redis->rpop("formid_{$order['openid']}");
            $status = $WeChat->sendMsg($SendWeChatMessage->receiveGoods($order['openid'], $formid, $order['out_trade_no'], $tip, $order['create_time'], $order['send_time']));
            if ($status['errcode'] == 0 || $formid == false) {
                break;
            }
        }

        //查找商家,发送小程序消息
        $users = new User();
        $andWhere = " id = {$order['mid']} ";
        $store = $users->storeOne($andWhere);
        while (true) {
            $formid_store = $redis->rpop("formid_{$store['openid']}");
            $status_store = $WeChat->sendMsg($SendWeChatMessage->receiveGoods($store['openid'], $formid_store, $order['out_trade_no'], $tip, $order['create_time'], $order['send_time']));
            if ($status_store['errcode'] == 0 || $formid_store == false) {
                break;
            }
        }
    }

    /**
     * 查找物流信息
     * @return \yii\web\Response
     */
    public function actionLogistic()
    {
        $request = Yii::$app->request;
        $out_trade_no = $request->post('out_trade_no');
        $order_num = $request->post('order_num');

        $msg = '';
        $flag = false;
        if (!$out_trade_no) {
            $msg = 'out_trade_no 不能为空';
        } elseif (!$order_num) {
            $msg = 'order_num 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $users = new User();
            $res = $users->checkLogistic($order_num);

            if ($res) {
                try {
                    if (json_decode($res, true)['status'] == 200) {
                        $map = " info = '{$res}'";
                        $where = " out_trade_no = '{$out_trade_no}' AND order_num = '{$order_num}' ";
                        $users->orderLogistic($map, $where);
                        $jsonData = ResponseHelper::buildJsonData(200, '成功', json_decode($res, true));
                    } else {
                        $jsonData = ResponseHelper::buildJsonData(400, json_decode($res, true)['message']);
                    }
                } catch (\Exception $e) {
                    $jsonData = ResponseHelper::buildJsonData(400, '订单号有误,联系商家');
                }
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '订单号有误.联系商家');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * 提现说明
     * @return \yii\web\Response
     */
    public function actionExplain()
    {
        $users = new User();
        $where = " style = 2 ";
        $explain = $users->presentExplain($where);
        if ($explain) {
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $explain);
        } else {
            $jsonData = ResponseHelper::buildJsonData(400, '失败');
        }
        return $this->asJson($jsonData);
    }

    /**
     * 消息
     * @return \yii\web\Response
     */
    public function actionNotice()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $style = $request->post('style'); //消息类型
        $status = $request->post('status'); //是否读取过

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (empty(isset($style))) {
            $msg = 'style 不能为空';
        } elseif (empty(isset($status))) {
            $msg = 'status 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;

            $where = " uid = {$uid} ";
            if ($status != 0) {
                $where .= " AND status = 1 ";
            }
            if ($style != 0) {
                $where .= " AND style = {$style} ";
            }
            $users = new User();
            $noticeList = $users->userNotice($where, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $noticeList);
        }

        return $this->asJson($jsonData);
    }

    /**
     * 让消息变为已读
     * @return \yii\web\Response
     */
    public function actionRead()
    {
        $request = Yii::$app->request;
        $nid = $request->post('nid');
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$nid) {
            $msg = 'nid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $users = new User();
            $noticeId = json_decode($nid, true);
            foreach ($noticeId as $key => $value) {
                if (!array_key_exists('id', $value)) {
                    $jsonData = ResponseHelper::buildJsonData(400, '缺少key值:id');
                    return $this->asJson($jsonData);
                }
            }

            try {
                foreach ($noticeId as $_key => $_val) {
                    $where = " uid = {$uid} AND id = {$_val['id']} ";
                    $users->noticeRead($where);
                }
                $jsonData = ResponseHelper::buildJsonData(200, '成功');
            } catch (\Exception $e) {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }

        return $this->asJson($jsonData);
    }

    /**
     * 生成带参数的二维码
     * @return \yii\web\Response
     */
    public function actionQrcode()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (empty(isset($mid))) {
            $msg = 'mid 不能为空';
        } elseif (empty(isset($uid))) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $WeChat = new WeChat();
            $dir = "teach/web/QRCode/{$uid}/{$mid}/";
            $scene = "mid={$mid}&user={$uid}";
            $page = "shopPage/shoppages/index/index";
            $QRCode = $WeChat->qrCode($dir, $scene, $page);
            $path = Yii::$app->params['QRCodeUrl'] . $dir . $QRCode;
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $path);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 获取用户积分记录
     * @return \yii\web\Response
     */
    public function actionScore()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $users = new User();
            $map = " uid = {$uid} AND mid = {$mid} AND status = 1 ";
            $scoreList = $users->userScoreRecord($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $scoreList);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 获取用户充值支票记录
     * @return \yii\web\Response
     */
    public function actionRecharge()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $users = new User();
            $map = " uid = {$uid} AND mid = {$mid} AND status = 1 ";
            $rechargeList = $users->userRechargeRecord($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $rechargeList);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 需要跳转的小程序
     * @return \yii\web\Response
     */
    public function actionSkip()
    {
        $request = Yii::$app->request;

        $mid = $request->post('mid');
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;

        if (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            if ($mid == $uid) {
                $data = [
                    'appid' => Yii::$app->params['serviceNum']['10000']['skip'],
                    'path' => Yii::$app->params['serviceNum']['10000']['path'],
                    'mid' => $mid,
                    'uid' => $uid,
                ];
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $data);
                return $this->asJson($jsonData);
            }
            $appid = Yii::$app->params['serviceNum'][$mid]['skip'];
            $path = Yii::$app->params['serviceNum'][$mid]['path'];
            if (!$appid || !$path) {
                $jsonData = ResponseHelper::buildJsonData(400, 'appid 或者 path没有配置');
                return $this->asJson($jsonData);
            }
            $data = [
                'appid' => $appid,
                'path' => $path,
                'mid' => $mid,
                'uid' => $uid,
            ];
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $data);

        }
        return $this->asJson($jsonData);
    }

    public function actionRefund()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $openid = $request->post('openid');
        $mid = $request->post('mid');
        $oid = $request->post('oid');
        $types = $request->post('types');
        $reason = $request->post('reason');
        $amount = $request->post('amount');
        $explain = $request->post('explain');
        $out_trade_no = $request->post('out_trade_no');

        $msg = '';
        $flag = false;

        if (!$uid) {
            $msg = 'uid 用户id不用为空';
        } elseif (!$openid) {
            $msg = 'openid不能为空';
        } elseif (!$mid) {
            $msg = '商家mid不能为空';
        } elseif (!$oid) {
            $msg = '订单id不能为空';
        } elseif (!$types) {
            $msg = '退款类型不能为空';
        } elseif (!$reason) {
            $msg = '申请原因不能为空';
        } elseif (!$amount) {
            $msg = '金额不能为空';
        } elseif (!$explain) {
            $msg = '申请说明不能为空';
        } elseif (!$out_trade_no) {
            $msg = '订单号不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $users = new User();

            //查找订单是否存在
            $andWhere = " uid = {$uid} AND out_trade_no = '{$out_trade_no}'";
            $order = $users->orderOne($andWhere);

            if (!in_array($order['status'], [1,2,3])) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单有误,核实后再申请');
                return $this->asJson($jsonData);
            }

            $map = [
                'uid' => $uid,
                'openid'=> $openid,
                'mid' => $mid,
                'oid' => $oid,
                'out_trade_no' => $out_trade_no,
                'types' => $types,
                'reason' => $reason,
                'amount' => $amount,
                'explain' => $explain,
                'img' => $request->post('img'),
                'status' => 1,
                ];

            $res = $users->userRefund($map, $oid, $out_trade_no);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '已成功发送申请,请耐心等待');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '申请失败');
            }
        }
        return $this->asJson($jsonData);
    }
}